﻿using System;
using System.Linq.Expressions;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using FluentDml.Tests.TestClasses;
using NHibernate.Linq;
using NUnit.Framework;

namespace FluentDml.Tests
{
    [TestFixture]
    public class DeleteTests
    {
        private static InMemoryDatabase _db;

        [TestFixtureSetUp]
        public void FixtureSetup()
        {
            _db = new InMemoryDatabase();
        }

        [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            _db.Dispose();
        }

        [SetUp]
        public void Setup()
        {
            _db.CreateSession();

            IList<Person> persons = TestData.GenerateSession<Person>().List<Person>(100).Get();

            foreach (var person in persons)
            {
                _db.Session.Save(person);
            }

            _db.Session.Flush();
        }

        [TearDown]
        public void TearDown()
        {
            _db.TearDownSession();
        }


        [Test]
        public void Delete_Where_Name_Equals_James()
        {
            Expression<Func<Person, bool>> where = p => p.FirstName == "James";

            int count = _db.Session.Query<Person>().Where(where).Count();

            string generateCommand = _db.Session.Delete<Person>(where)
                .GenerateCommand();

            int executeCommand = _db.Session.Delete<Person>(where)
                .ExecuteCommand();

            int count3 = _db.Session.Query<Person>().Where(where).Count();
            int count4 = _db.Session.Query<Person>().Count();

            Assert.AreNotEqual(0, count4, "should not delete all people in the DB");
            Assert.AreEqual(0, count3);
        }

        [Test]
        public void Delete_Where_Name_Contains_ame()
        {
            Expression<Func<Person, bool>> where = p => p.FirstName.Contains("ame");

            int count = _db.Session.Query<Person>().Where(where).Count();

            string generateCommand = _db.Session.Delete<Person>(where)
                .GenerateCommand();

            int executeCommand = _db.Session.Delete<Person>(where)
                .ExecuteCommand();

            int count3 = _db.Session.Query<Person>().Where(where).Count();
            int count4 = _db.Session.Query<Person>().Count();

            Assert.AreNotEqual(0, count4, "should not delete all people in the DB");
            Assert.AreEqual(0, count3);
        }

        [Test]
        public void Delete_Where_Name_StartsWith_Jam()
        {
            Expression<Func<Person, bool>> where = p => p.FirstName.StartsWith("Jam");

            int count = _db.Session.Query<Person>().Where(where).Count();

            string generateCommand = _db.Session.Delete<Person>(where)
                .GenerateCommand();

            int executeCommand = _db.Session.Delete<Person>(where)
                .ExecuteCommand();

            int count3 = _db.Session.Query<Person>().Where(where).Count();
            int count4 = _db.Session.Query<Person>().Count();
            
            Assert.AreNotEqual(0, count4, "should not delete all people in the DB");
            Assert.AreEqual(0, count3);
        }
    }
}


